package com.ruoyi.weixin.mp.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.weixin.mp.entity.WxMsg;
import com.ruoyi.weixin.mp.form.WxMsgReplyForm;
import com.ruoyi.weixin.mp.service.MsgReplyService;
import com.ruoyi.weixin.mp.service.WxMsgService;
import com.ruoyi.weixin.mp.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 微信消息
 *
 * @author niefy
 * @date 2020-05-14 17:28:34
 */
@RestController
@RequestMapping("/manage/wxMsg")
@Api(tags = {"公众号消息记录-管理后台"})
public class WxMsgManageController {

	@Autowired
	private WxMsgService wxMsgService;
	@Autowired
	private MsgReplyService msgReplyService;

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@PreAuthorize("@ss.hasPermi('wx:wxmsg:list')")
	@ApiOperation(value = "列表")
	public AjaxResult list(@RequestParam Map<String, Object> params){
		PageUtils page = wxMsgService.queryPage(params);
		return AjaxResult.success(page);
	}


	/**
	 * 信息
	 */
	@GetMapping("/info/{id}")
	@PreAuthorize("@ss.hasPermi('wx:wxmsg:info')")
	@ApiOperation(value = "详情")
	public AjaxResult info(@PathVariable String appid,@PathVariable("id") Long id){
		WxMsg wxMsg = wxMsgService.getById(id);
		return AjaxResult.success(wxMsg);
	}

	/**
	 * 回复
	 */
	@PostMapping("/reply")
	@PreAuthorize("@ss.hasPermi('wx:wxmsg:save')")
	@ApiOperation(value = "回复")
	public AjaxResult reply(@PathVariable String appid,@RequestBody WxMsgReplyForm form){
		msgReplyService.reply(form.getOpenid(),form.getReplyType(),form.getReplyContent());
		return AjaxResult.success();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@PreAuthorize("@ss.hasPermi('wx:wxmsg:delete')")
	@ApiOperation(value = "删除")
	public AjaxResult delete(@PathVariable String appid,@RequestBody Long[] ids){
		wxMsgService.removeByIds(Arrays.asList(ids));
		return AjaxResult.success();
	}

}
